<?php
class MemberViewModel extends ViewModel{
	
	public $viewFields = array(
		'Membership'=>array('*','_as'=>'p'),//,'_type'=>'LEFT'),
        //'Shequ'=>array('sqname','_as'=>'sq','_on'=>'p.shequid = sq.id','_type'=>'LEFT'),
        //'city'=>array('cid','cname','_as'=>'ct','_on'=>'sq.cid=ct.cid','_type'=>'LEFT'), 
        //'section'=>array('qid','qname','_as'=>'sec','_on'=>'sq.qid=sec.qid','_type'=>'LEFT'),
        //'street'=>array('jid','jname','_as'=>'st','_on'=>'sq.jid=st.jid'), 
	);

	/**
	 * 获取会员的基本信息及账号金额
	 * @param array $where
	 * @param string $limit
	 * @param string $order
	 * @return Ambigous <number, unknown>
	 */
    public function getAll($where,$limit,$order){
		$result = $this->where($where)->limit($limit)->order($order)->select();
		foreach($result as $key=>$val){
			$withdraw = D('MemberWithdraw')->getAllMoney($val['uid']);
			$result[$key]['withdraw'] = $withdraw?$withdraw:0;			
			
			$account = M('member_moneylog')->where(array('uid'=>$val['uid']))->order('member_moneylog_id desc')->field('active_money,freeze_money')->find();
			$result[$key]['account_money'] = $account?$account:0;
			
			//获取会员交易评分
			$point = D('Appraise')->getAverage($val['uid']);
			//$allpoint = M('appraise')->where(array('uid'=>$val['uid']))->sum('point');
			//echo $val['uid']."dd".$point."<br/>";
			$result[$key]['point']=$point;
			
		}
		//die;
		return $result;
	}

    public function getOne($where){
		return $this->where($where)->find();
	}
	
	//获取指定条件所有信息
	public function getMAll($where){
		$result = $this->where($where)->select();
		foreach($result as $key=>$val){
			$withdraw = D('MemberWithdraw')->getAllMoney($val['uid']);
			$result[$key]['withdraw'] = $withdraw?$withdraw:0;
				
			$account = M('member_moneylog')->where(array('uid'=>$val['uid']))->order('member_moneylog_id desc')->field('active_money,freeze_money')->find();
			//echo M('member_moneylog')->getLastSql()."<br/>";
			$result[$key]['account_money'] = $account?$account:0;
		}
		//die;
		return $result;
	}
	
} 

?>
